module Matrix_module
	implicit none
contains
	subroutine	matrixInverse(matrix, inv_matrix)
		real, allocatable, dimension(:,:), intent(in)	::	matrix
		real, allocatable, dimension(:,:), intent(inout):: 	inv_matrix
		real, allocatable, dimension(:,:)				:: 	temp_matrix
		integer		::	rank
		integer		::	i, j, index_of_pivot
		integer		::	m,n

		rank = size(matrix, dim=1)
		allocate(inv_matrix(rank, rank))
		temp_matrix = matrix
		!initializing the inv_matrix to a identity matrix
		do i = 1, rank
			inv_matrix(i,:) = (/(0,j = 1, rank)/)
			inv_matrix(i,i) = 1
		end do
		!generating the diagonal form selecting the pivot
		do j = 1, rank
			if( temp_matrix(j,j) == 0	) then
				print*,"No solution"
				return
			end if		
			inv_matrix(j,:) = inv_matrix(j,:)/temp_matrix(j,j)
			temp_matrix(j,:) = temp_matrix(j,:)/temp_matrix(j,j)
			do i = 1, rank
				if(i == j) cycle
				inv_matrix(i,:) = inv_matrix(i,:) - temp_matrix(i,j)*inv_matrix(j,:)
				temp_matrix(i,:) = temp_matrix(i,:) - temp_matrix(i,j)*temp_matrix(j,:)
			end do
		end do
	end subroutine matrixInverse
end module  Matrix_module
